package com.atguigu.gulimall.ware.listener;

import com.atguigu.gulimall.ware.entity.WareOrderTaskDetailEntity;
import com.atguigu.gulimall.ware.service.WareOrderTaskService;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.to.OrderTo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@RabbitListener(queues = "stock.release.stock.queue")
@Service
public class WareListener {
    @Autowired
    private WareOrderTaskService wareOrderTaskService;
    @Autowired
    private WareSkuService wareSkuService;
    /**
     * 接收订单系统主动发送的消息
     * @param message
     * @param channel
     * @param orderTo
     */
    @RabbitHandler
    public void handleOrder(Message message, Channel channel, OrderTo orderTo) throws IOException {
        Integer status = orderTo.getStatus();
        if(status==4){
            //订单已经取消
            //查询订单工作单没有解锁的订单号
            List<WareOrderTaskDetailEntity> taskDetailEntities = wareOrderTaskService.orderTaskStatus(orderTo);
            //解锁库存
            wareSkuService.unlock(taskDetailEntities);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
        else{
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}
